package com.hemaapp.yjnh.nettask;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import xtom.frame.exception.DataParseException;

import com.google.gson.Gson;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.yjnh.BaseApplication;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.BaseNetTask;
import com.hemaapp.yjnh.bean.CouponRecord;
import com.hemaapp.yjnh.bean.User;

/**获取粮票的各种记录列表
 * 
 */
public class CouponRecordListTask extends BaseNetTask {

	public CouponRecordListTask(BaseHttpInformation information,
			HashMap<String, String> params) {
		super(information, params);
	}

	public CouponRecordListTask(BaseHttpInformation information,
			HashMap<String, String> params, HashMap<String, String> files) {
		super(information, params, files);
	}

	@Override
	public Object parse(JSONObject jsonObject) throws DataParseException {
		getCostGainTotal(jsonObject);
		return new Result(jsonObject);
	}

	private class Result extends HemaPageArrayResult<CouponRecord> {

		public Result(JSONObject jsonObject) throws DataParseException {
			super(jsonObject);
		}

		@Override
		public CouponRecord parse(JSONObject jsonObject) throws DataParseException {
			return new Gson().fromJson(jsonObject.toString(), CouponRecord.class);
		}
	}
	
	/**获取消费和获得的消费总数
	 * 
	 */
	private void getCostGainTotal(JSONObject jsonObject) {
		User user = BaseApplication.getInstance().getUser();
		try {
			if (!jsonObject.isNull("infor")
					&& !isNull(jsonObject.getString("infor"))) {
				JSONObject object = jsonObject.getJSONObject("infor");
				if (!object.isNull("totalCost")) {
					String totalCost = object.getString("totalCost");
					user.setCouponCost(totalCost);
				}
				if (!object.isNull("totalGain")) {
					String totalGain = object.getString("totalGain");
					user.setCouponGain(totalGain);
				}
			}
		} catch (JSONException e) {
			
		}
	}
}
